package cs5226.tags;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import cs5226.util.*;
import cs5226.model.*;

public class LastValue extends SimpleTagSupport {
	
	private String statistics;

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = getJspContext().getOut();
		
		if (StringUtil.isNullOrBlank(statistics)) {			
			out.println ("No statistics specified!");
			return;
		}
		
		List<? extends SingleValueStatistics> statList = (List<? extends SingleValueStatistics>) 
				this.getJspContext().findAttribute(statistics);
		if (statList == null || statList.size() == 0) {
			out.println ("No statistics found!");
			return;
		}
		
		SingleValueStatistics stat = statList.get(0);
		
		
		PageContext pc = (PageContext) getJspContext();
		HttpServletRequest request = (HttpServletRequest) pc.getRequest();
		out.println(stat.toHtml() + " " + ThresholdUtil.getHtmlHealthStatus(statistics, request, stat)); 	
	}

	public String getStatistics() {
		return statistics;
	}

	public void setStatistics(String statistics) {
		this.statistics = statistics;
	}
	
}
